<?php
require_once(LIB.'/class_Categories.php');
//require_once(LIB.'/class_Content.php');
if (!defined('TB_CATEGORIES')){ define('TB_CATEGORIES','categories');}
class manage_device_controller{
    private $categories;
    private $structure;
    function __construct() {
        $categories=new categories();
        $categories->get_full_list();
        $cat_structure=$categories->open_categories_tree(0,0);
        array_shift($cat_structure);   
        $this->structure=$cat_structure;
        $this->categories=$categories;                
    }
    
    
    public function exec_index(){
       $content=$this->show_index_device();
       return array('content'=>$content);
    }
    
    public function exec_create(){
       $content=$this->show_create_device();
       return array('content'=>$content);
    }
    
    public function exec_view_device(){
       $content=$this->show_view_device();
       return array('content'=>$content);
    }
    public function exec_edit(){
       $content=$this->show_edit_device();
       return array('content'=>$content);
    }
    
    public function exec_delete(){
        global $dbdefault;
        $d_id = GET_value('d_id');
        
        $query=  "update devices set status=0 where iddevices=$d_id"; 

        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            //return array('success'=>TRUE);
            header('Location: index.php?m=manage_device');
        }
    }
    
    private function show_edit_device(){
        global $dbdefault;

        $qry="SELECT idcustomers, namecustomer FROM customers";
       
       $res1 = mysql_query($qry,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
       $posts=array();
        while($arr = mysql_fetch_assoc($res1)){
            $posts[] = $arr;            
        }
        
       $d_id =  GET_value('d_id','');
       $query=sprintf("SELECT c.*, d.category_name FROM (SELECT a.*, b.namecustomer FROM devices a JOIN customers b 
            ON a.`idcustomer`=b.`idcustomers` WHERE a.status=1 AND a.`iddevices`=%d) c JOIN categories d ON 
            c.groupdevice=d.category_id",  qs($d_id));
       
       $res = mysql_query($query,$dbdefault);
       $row = mysql_fetch_array($res); 
       
       
       $posts_tpl = new Template(MODULE_ROOT.'/manage_device/view/edit_form.html');
        $posts_tpl ->set('row', $row);
       $posts_tpl->set('categories',$this->categories->categories); 
       $posts_tpl->set('structure',$this->structure);  
       $posts_tpl ->set('posts', $posts);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_device/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
       
    }


    private function show_view_device(){
         global $dbdefault;
        $d_id =  GET_value('d_id','');
         //Thong tin cau hinh lien quan
         $qry=sprintf("SELECT idconfigurations, configname FROM configurations WHERE iddevice=%d",  qs($d_id));
         $resqry = mysql_query($qry,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
        $posts=array();
        while($arr = mysql_fetch_assoc($resqry)){
            $posts[] = $arr;            
        }
        //Thong tin device 
         
        $query=sprintf("SELECT c.*, d.category_name FROM (SELECT a.*, b.namecustomer FROM devices a JOIN customers b 
            ON a.`idcustomer`=b.`idcustomers` WHERE a.status=1 AND a.`iddevices`=%d) c JOIN categories d ON 
            c.groupdevice=d.category_id",  qs($d_id));
       
        $res = mysql_query($query,$dbdefault);
       $row = mysql_fetch_array($res);  
       
       $posts_tpl = new Template(MODULE_ROOT.'/manage_device/view/view_detail.html');
        $posts_tpl ->set('row', $row);
        $posts_tpl ->set('posts', $posts);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_device/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    private function show_create_device(){
        global $dbdefault;
        $query="SELECT idcustomers, namecustomer FROM customers";
       
       $res = mysql_query($query,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
       $posts=array();
        while($arr = mysql_fetch_assoc($res)){
            $posts[] = $arr;            
        }
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_device/view/create_form.html');
       $posts_tpl->set('categories',$this->categories->categories); 
       $posts_tpl->set('structure',$this->structure);  
       $posts_tpl ->set('posts', $posts);
       $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_customer/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);
        return $tpl ->fetch(); 
    }


    private function show_index_device(){
        global $dbdefault;
        $s =  GET_value('s','');
        $group =  GET_value('group','0');
        $cus =  GET_value('cus','');
        $Where="";
        if($s!=""){
            $Where=$Where." and namedevice like '%$s%'";
        }
        if($group!="0"){
            $Where=$Where." and groupdevice='$group'";
        }
        if($cus!=""){
            $Where=$Where." and idcustomer='$cus'";
        }
        
        $item_per_page = 20;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        
        
        $qry="SELECT count(*) AS total FROM devices where status=1".$Where; 
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);
        
        
        //$query="select * from devices order by iddevices DESC LIMIT $limit, $offset";  
        $query="SELECT c.*, d.category_name FROM (SELECT a.*, b.namecustomer FROM devices a JOIN customers b 
            ON a.`idcustomer`=b.`idcustomers` where a.status=1".$Where.") c JOIN categories d ON c.groupdevice=d.category_id  
            order by iddevices DESC LIMIT $limit, $offset";
        //echo $query;
        $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".  mysql_error());
        $posts = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $posts[]=$arr;
        }
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_device/view/view_index.html');
        $posts_tpl ->set('posts', $posts);
        $posts_tpl->set('page_num', $page_num);
        $posts_tpl->set('num_pages', $num_pages);
        $posts_tpl->set('group', $group);
        $posts_tpl->set('cus', $cus);
        $posts_tpl->set('s', $s);
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_left_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_device/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
   private function show_left_menu($action=''){
       global $dbdefault;
       $group =  GET_value('group','0');
       $query="SELECT a.*, b.category_name FROM (SELECT groupdevice, COUNT(*) AS total FROM devices where status=1 GROUP BY groupdevice) a JOIN categories b ON a.groupdevice=b.`category_id`"; 
       $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".  mysql_error());
        $postsl = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $postsl[]=$arr;
        }
        
        if (empty($action)) $action =  GET_value('a','index');
        $tpl = new Template(MODULE_ROOT.'/manage_device/view/left_menu.html');
        $tpl->set('a',$action);
        $tpl->set('postsl',$postsl);
        $tpl->set('group',$group);
        return $tpl->fetch();
    }
    
    //-----------------------------------AJAX--------------------------------------------------
    public function ajax_create(){
        global $dbdefault;
        $device_name = GET_value('device_name');
        $deliver_date = GET_value('deliver_date');
        $part_number = GET_value('part_number');
        $warranty_todate = GET_value('warranty_todate');
        $serial_number = GET_value('serial_number');
        $detail = GET_value('detail');
        $provided_by = GET_value('provided_by');
        $device_type = GET_value('device_type');
        $device_customer = GET_value('device_customer');

        $user_id=$_SESSION['user_id'];
        
        $query=  "insert into devices (namedevice, details, partnumber, serial, timestart,providedby,
            warrantyto, groupdevice, idcustomer, time_create, user_create) 
            values('$device_name','$detail','$part_number','$serial_number',STR_TO_DATE('$deliver_date','%m/%d/%Y'),
                '$provided_by',STR_TO_DATE('$warranty_todate','%m/%d/%Y'),$device_type,$device_customer,NOW(),$user_id)";
        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
    
    public function ajax_update(){
        global $dbdefault;
        $d_id = GET_value('d_id');
        $device_name = GET_value('device_name');
        $deliver_date = GET_value('deliver_date');
        $part_number = GET_value('part_number');
        $warranty_todate = GET_value('warranty_todate');
        $serial_number = GET_value('serial_number');
        $detail = GET_value('detail');
        $provided_by = GET_value('provided_by');
        $device_type = GET_value('device_type');
        $device_customer = GET_value('device_customer');

        $user_id=$_SESSION['user_id'];
        
        $query=  "update devices set namedevice='$device_name', details='$detail', partnumber='$part_number', 
            serial='$serial_number', timestart=STR_TO_DATE('$deliver_date','%m/%d/%Y'),providedby='$provided_by',
            warrantyto=STR_TO_DATE('$warranty_todate','%m/%d/%Y'), groupdevice=$device_type, 
                idcustomer=$device_customer, time_create=NOW(), user_create=$user_id where iddevices=$d_id"; 

        
        $res = mysql_query($query,$dbdefault);
        if(!$res){
            return array('success'=>FALSE, 'error'=>$query);
        }else{
            return array('success'=>TRUE);
        }
    }
}
?>
